package com.ruoyi.teaching.listener;

import com.ruoyi.teaching.bo.InterviewFormMqDataBo;
import com.ruoyi.teaching.bo.InterviewFromMqBo;
import com.ruoyi.teaching.index.InterviewIndex;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2024/4/22
 */
@Component
public class InterviewListener {
    public static final String HIGH_INTERVIEW_TABLE = "ya_high_interview";
    public static final String INSERT = "INSERT";
    public static final String UPDATE = "UPDATE";
    public static final String DELETE = "DELETE";
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "edu.interview.es.queue"),
            exchange = @Exchange(name = "edu.interview.exchange", type = ExchangeTypes.TOPIC),
            key = {"interview.update","interview.insert"}
    ))
    public void updateInterview(HighInterview highInterview){

        InterviewIndex interviewIndex = new InterviewIndex();
        interviewIndex.setAll(highInterview.getQuestion() + highInterview.getKnowledgePoint());
            BeanUtils.copyProperties(highInterview, interviewIndex);

        elasticsearchRestTemplate.save(interviewIndex);
        System.out.println("开始更新面试题es索引");
    }*/


   /* @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "edu.interview.es.queue"),
            exchange = @Exchange(name = "edu.interview.exchange", type = ExchangeTypes.TOPIC),
            key = {"interview.delete"}
    ))
    public void deleteInterview(List<Long> ids){

        for (int i = 0; i < ids.size(); i++) {
            elasticsearchRestTemplate.delete(String.valueOf(ids.get(i)),InterviewIndex.class);
        }

        System.out.println("开始删除面试题es索引");
    }*/




    private AtomicInteger integer = new AtomicInteger(0);
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "edu.interview.es.queue.canal"),
//            exchange = @Exchange(name = "edu.interview.exchange.canal", type = ExchangeTypes.TOPIC),
//            key = {"interview"}
//    ))
    public void updateInterview(InterviewFromMqBo interviewFromMqBo){
        System.out.println(integer.addAndGet(interviewFromMqBo.getData().size()));
        //判断操作的表是不是high_interview
        String table = interviewFromMqBo.getTable();
        if (!HIGH_INTERVIEW_TABLE.equals(table)) {
            return;
        }
        //判断操作类型是不是insert,update/delete
        String type = interviewFromMqBo.getType();
        List<InterviewFormMqDataBo> data = interviewFromMqBo.getData();
        if (INSERT.equals(type) || UPDATE.equals(type) ) {
            //遍历->转换
            List<InterviewIndex> interviewIndices = data.stream().map(bo -> {
                InterviewIndex interviewIndex = new InterviewIndex();
                interviewIndex.setAll(bo.getQuestion());
                interviewIndex.setImportance(bo.getImportance());
                interviewIndex.setStage(bo.getStage());
                interviewIndex.setQuestion(bo.getQuestion());
                interviewIndex.setAnswer(bo.getAnswer());
                interviewIndex.setId(bo.getId());
                return interviewIndex;
            }).collect(Collectors.toList());
            elasticsearchRestTemplate.save(interviewIndices);
        }else if (DELETE.equals(type)) {
            data.forEach(bo->{
                Long id = bo.getId();
                elasticsearchRestTemplate.delete(id.toString(),InterviewIndex.class);
            });
        }

    }
}
